//
//  TXFComposeCollection.swift
//  weibo飞飞
//
//  Created by 田侠飞 on 16/7/3.
//  Copyright © 2016年 田侠飞. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class TXFComposeCollection: UICollectionView {

    
    //定义一个可变数据来接受图片
    
    var  imageList: [UIImage] =  [UIImage]()
    
    //定义闭包
    
   var clouseImageCallBack:(()->())?
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
    }

  required init?(coder aDecoder: NSCoder) {
      fatalError("init(coder:) has not been implemented")
  }
    func addImages(image: UIImage){
    
    imageList.insert(image, atIndex: 0)
        if imageList.count > 9 {
        imageList.removeLast()
        
        }
    
    //记得刷新
        
        reloadData()
    }
    
    
    
   func setupUI(){

    backgroundColor = UIColor.whiteColor()
    
    registerClass(TXFComposePictureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
 
    let  itemMagein : CGFloat = 5
    let  layout = collectionViewLayout as! UICollectionViewFlowLayout
    layout.minimumInteritemSpacing = itemMagein
    
    layout.minimumLineSpacing = itemMagein

    let cellWH : CGFloat =  (SCREENW - 20  - itemMagein*2)/3
    
    layout.itemSize = CGSizeMake(cellWH, cellWH)
    
   dataSource = self
   delegate = self
    }

}
extension TXFComposeCollection: UICollectionViewDelegate,UICollectionViewDataSource {
   

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let count = imageList.count
        if count == 0 || count == 9 {
        return count
            
        }else{
        
        
        return imageList.count + 1
        
        }
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)as! TXFComposePictureCell
       
        if imageList.count == indexPath.item {
            cell.image = nil
            
        }else{
            
            
            cell.image = imageList[indexPath.item]
            
        }
        
        
        //执行闭包
        
        cell.callbackDeleteBtn = {
        
        //删除数组中的元素
            
            self.imageList.removeAtIndex(indexPath.item)
        
        
//            if self.imageList.count == 0 {
//            
//            self.hidden = true
//            
//            }
            //
            self.reloadData()
        
        }
        
        return cell
        
    }
    
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        collectionView.deselectItemAtIndexPath(indexPath, animated: true)
        if imageList.count == indexPath.item{
        clouseImageCallBack?()
        
        }
        
    }
}




class TXFComposePictureCell: UICollectionViewCell {
    
    //定义闭包 告知删除图片
    
    var callbackDeleteBtn: (()->())?
    
    //设置图片恭外界调用
    
    var image: UIImage?{
        didSet{
        
            if image == nil {
            
            BgImageView.image = UIImage(named: "compose_pic_add")
            BgImageView.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
                
                //一开始时候删除按钮隐藏
            deleteButton.hidden = true
            
            }else{
            
            BgImageView.image = image
            BgImageView.highlightedImage = nil
            deleteButton.hidden = false
            }
        
        
        
        }
    
    
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
    
        
//    backgroundColor = RandomColor()
    contentView.addSubview(BgImageView)
    contentView.addSubview(deleteButton)
        BgImageView.snp_makeConstraints { (make) -> Void in
            
        make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
            
        }
    
        
      deleteButton.snp_makeConstraints { (make) -> Void in
        
        make.top.equalTo(contentView)
        
        make.trailing.equalTo(contentView)
        
        
        
        
        }
        
        
    }
    
    
    //删除按钮点击事件
    
    func deleteButtonClick(){
    
        print("hello")
    //调用闭包
        
        callbackDeleteBtn?()
    
    }
    
    
    //懒加载图片:

    private lazy var BgImageView : UIImageView = UIImageView()
    //懒加载删除图片的按钮:
    
    private lazy var deleteButton: UIButton = {
    
    let button = UIButton()
   
    button.setImage(UIImage(named: "compose_photo_close"), forState: UIControlState.Normal)
  
     button.addTarget(self, action: "deleteButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
    return button
    }()
    

}



